/**
 * Copyright (c) 2021 九色 All rights reserved.
 *
 * https://www.chenglicun.io
 *
 * 版权所有，侵权必究！
 */

package io.chenglicun.modules.data.controller;

import io.chenglicun.common.annotation.LogOperation;
import io.chenglicun.common.constant.Constant;
import io.chenglicun.common.page.PageData;
import io.chenglicun.common.utils.Result;
import io.chenglicun.common.validator.AssertUtils;
import io.chenglicun.modules.data.entity.DataGridVillageEntity;
import io.chenglicun.modules.data.entity.DataServiceEntity;
import io.chenglicun.modules.data.service.DataGridVillageService;
import io.chenglicun.modules.data.service.DataServiceService;
import io.chenglicun.modules.data.utils.ExcelUtils;
import io.chenglicun.modules.pay.dto.OrderDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;


/**
* 订单
*
* @author Mark sunlightcs@gmail.com
*/
@RestController
@RequestMapping("data/serivce")
@Api(tags="公共服务信息")
public class DataServiceController {
    @Autowired
    private DataServiceService dataServiceService;

    @PostMapping("excel")
    @ApiOperation("导入公共服务信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "file", value = "导入的文件", paramType = "file", required = true, dataType="__file")
    })
    public Result excel(@ApiIgnore @RequestParam("file") MultipartFile file){
        try {
            List<List<Object>> lists = ExcelUtils.praseExcel(file.getInputStream());
            if (lists==null||lists.isEmpty()){
                return new Result().error(203,"execl数据为空");
            }
            dataServiceService.excelData(lists);
        }catch (Exception e){
            return new Result().error(203);
        }
        return  new Result();
    }



    @PostMapping("page")
    @ApiOperation("查询公共服务信息带分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String"),
            @ApiImplicitParam(name ="type", value = "类型", paramType = "query", dataType="String")
    })
    public Result<PageData<DataServiceEntity>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        try {
            PageData<DataServiceEntity> page = dataServiceService.page(params);
            return new Result<PageData<DataServiceEntity>>().ok(page);
        }catch (Exception e){
            return new Result().error(203);
        }
    }

    @PostMapping("update")
    @ApiOperation("根据主键修改公共服务信息")
    @LogOperation("修改")
    public Result update(@RequestBody DataServiceEntity entity){
        dataServiceService.update(entity);
        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    public Result delete(@RequestBody Integer[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");
        dataServiceService.delete(ids);
        return new Result();
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    public Result save(@RequestBody DataServiceEntity entity){
        dataServiceService.save(entity);

        return new Result();
    }
}